# -*- coding: utf-8 -*-
from . import BaseController
from backend.libs import myjson as json
from backend.models import Category, Option


class List(BaseController):

    def get(self):
        c = Category.objects
        name = self.get_argument('name', '')
        if name:
            c = c.filter(name__contains=name)
        state = self.int_argument('state', 0)
        c = c.filter(state=state)
        c = c.order_by('show_order')
        number_per_page = self.int_argument('pageSize', 50)
        paginator = self.pagination(c.all(), number_per_page=number_per_page)
        return self.render_json(data=paginator)


class ListAll(BaseController):

    def get(self):
        result_type = self.int_argument('result_type', 0)
        state = self.int_argument('state', 0)
        c = Category.objects.filter(state=state)
        categories = c.all()
        if result_type == 1:
            rslt = [ {'value': cate.id, 'label': cate.name } for cate in categories]
        else:    
            rslt = [ {'value': '%s,%s' % (cate.name, cate.id)} for cate in categories]
        return self.render_json(data={'categories': rslt})


class BaseCategoryController(BaseController):

    def set_category_properties(self, category, data):
        category.state = data['state']
        category.name = data['name']
        category.show_order = data['show_order']
        category.save()
        return category

class Update(BaseCategoryController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='分类名称不能为空')
        category_id = data['id']
        category = Category.objects.filter(id=category_id).first()
        if not category:
            return self.render_json(code=1001, msg='分类不存在')
        category = self.set_category_properties(category, data)
        return self.render_json(data=category.to_dict())


class Create(BaseCategoryController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['name']:
            return self.render_json(code=1001, msg='分类名称不能为空')
        category = Category()
        category = self.set_category_properties(category, data)
        return self.render_json(data=category.to_dict())


class Delete(BaseController):

    def post(self, category_id):
        category_id = int(category_id)
        if category_id > 0:
            category = Category.objects.filter(id=category_id).first()
            if category:
                category.state = 1
                category.save()
        return self.render_json()


class ListOptions(BaseController):

    def get(self):
        category_id = self.int_argument('category_id', -1)
        if category_id <= 0:
            return self.render_json(code=1001, msg='分类ID不能为空')

        category = Category.objects.filter(id=category_id).first()
        if not category:
            return self.render_json(code=1001, msg='分类不能不存在')
        c = category.options
        title = self.get_argument('title', '')
        if title:
            c = c.filter(title__contains=title)
        state = self.int_argument('state', 0)
        c = c.filter(state=state)
        c = c.order_by('show_order')
        result_type = self.int_argument('result_type', 0)
        if result_type == 1:
            data = [ {'value': co.id, 'label': co.title } for co in c.all() ]
        else:
            number_per_page = self.int_argument('pageSize', 50)
            data = self.pagination(c.all(), number_per_page=number_per_page)
        return self.render_json(data=data)

     
class BaseCategoryOptionController(BaseController):

    def set_categoryoption_properties(self, category, categoryOption, data):
        categoryOption.state = data['state']
        categoryOption.title = data['title']
        categoryOption.desc = data['desc']
        categoryOption.show_order = data['show_order']
        categoryOption.save()

        category.options.add(categoryOption)
        category.save()
        return categoryOption


class UpdateOptions(BaseCategoryOptionController):

    def post(self):
        data = json.loads(self.request.body)
        if not data['title']:
            return self.render_json(code=1001, msg='分类选项标题不能为空')
        category_option_id = data['id']
        if not category_option_id:
            return self.render_json(code=1001, msg='分类选项ID不能为空')
        category_option = Option.objects.filter(id=category_option_id).first()
        if not category_option:
            return self.render_json(code=1001, msg='分类选项不存在')
        category_id = data['category_id']
        category = Category.objects.filter(id=category_id).first()
        if not category:
            return self.render_json(code=1001, msg='分类不存在')
        category_option = self.set_categoryoption_properties(category, category_option, data)
        return self.render_json(data=category_option.to_dict())


class CreateOptions(BaseCategoryOptionController):

    def post(self):
        data = json.loads(self.request.body)
        print(data)
        if not data['title']:
            return self.render_json(code=1001, msg='分类名称不能为空')
        category_id = data['category_id']
        category = Category.objects.filter(id=category_id).first()
        if not category:
            return self.render_json(code=1001, msg='分类不存在')
        category_option = Option()
        category_option = self.set_categoryoption_properties(category, category_option, data)
        return self.render_json(data=category_option.to_dict())


class DeleteOptions(BaseController):

    def post(self, category_option_id):
        category_option_id = int(category_option_id)
        if category_option_id > 0:
            category_option = Option.objects.filter(id=category_option_id).first()
            if category_option:
                category_option.state = 1
                category_option.save()
        return self.render_json()
